/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.lnu.ums.ui.converters;

import edu.lnu.ums.dal.IFacultyDAO;
import edu.lnu.ums.entities.Faculty;
import edu.lnu.ums.service.IDictionaryService;
import edu.lnu.ums.ui.TreeBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Renuald
 */
public class FacultyConverter implements Converter{
    private IDictionaryService dictionaryService;

    private TreeBean treeBean;

    public FacultyConverter() {
        try {
            dictionaryService = (IDictionaryService) InitialContext.doLookup("UMS/DictionaryService/local");
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Object getAsObject(FacesContext fc, UIComponent uic, String string) {
        if ("".equals(string)) {
            return null;
        }
        
        return dictionaryService.getFacultyByFullName(treeBean.getSelectedObject(), string);
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object o) {
        if (o == null) {
            return "";
        }

        return ((Faculty)o).getFullName();
    }

    public TreeBean getTreeBean() {
        return treeBean;
    }

    public void setTreeBean(TreeBean treeBean) {
        this.treeBean = treeBean;
    }

}
